<?php
/**
 * Created by PhpStorm.
 * User: 15272
 * Date: 2019/1/6
 * Time: 16:32
 */

namespace app\user\controller;


use app\admin\model\MoneyInfoModel;
use cmf\controller\UserBaseController;

class MoneyController extends UserBaseController
{
    //资金记录
    public function index()
    {
        $this->view->data = MoneyInfoModel::where('user_id', cmf_get_current_user_id())
            ->order('create_time', 'DESC')
            ->paginate(15, true);
        return $this->fetch();
    }

    //获取一个7天的收入数据
    public function getSevenSkyData()
    {
        $length_day = 7;
        $data = MoneyInfoModel::where('user_id', cmf_get_current_user_id())/*自己的数据*/
        ->order('m_id', 'desc')/*旧数据放在后面*/
        ->where('m_money', '>', 0)/*收入*/
        ->whereTime('create_time', '>', strtotime("-{$length_day} days"))/*7天内的数据*/
        ->select()->toArray();

        $data_list = [];
        $length_day_copy = $length_day - 1;
        //添加7天的日期
        while ($length_day_copy != 0) {
            $data_list[date('Y-m-d',strtotime("-{$length_day_copy} days"))] = [];
            $length_day_copy--;
        }
        //日期分组
        foreach ($data as $datum) {
            $data_list[date('Y-m-d', $datum['create_time'])][] = $datum;
        }
        //价格统计
        $data_sum = [];
        foreach ($data_list as $key => $item) {
            $money_sum = 0;
            if (count($item) > 0) {
                $money_sum = array_sum(array_column($item, 'm_money'));
            }
            $data_sum[] = [
                'money' => $money_sum,
                'name' => $key
            ];


        }


        $this->success('', '', $data_sum);
    }
}